﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace AssignmentsManage.Windows
{
    /// <summary>
    /// Interaction logic for LinkedFilesWindow.xaml
    /// </summary>
    public partial class LinkedFilesWindow : Window
    {
        public LinkedFilesWindow()
        {
            InitializeComponent();
            LinkedFiles = new ObservableCollection<DataClasses.LinkedFile>();
            //WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = System.Globalization.CultureInfo.GetCultureInfo("fr");
        }

        #region Properties

        public ObservableCollection<DataClasses.LinkedFile> LinkedFiles
        {
            get;
            set;
        }

        #endregion

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "All Files (*.*)|*.*";
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == true)
            {
                foreach (string fileName in ofd.FileNames)
                {
                    LinkedFiles.Add(new DataClasses.LinkedFile(fileName));
                }
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void lstFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnRemove.IsEnabled = lstFiles.SelectedItems.Count > 0;
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            for (int i = lstFiles.SelectedItems.Count - 1; i >= 0; i--)
            {
                LinkedFiles.Remove((DataClasses.LinkedFile)lstFiles.SelectedItems[i]);
            }
        }

        private void lstFiles_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];

                foreach (string droppedFilePath in droppedFilePaths)
                {
                    LinkedFiles.Add(new DataClasses.LinkedFile(droppedFilePath));
                }
            }
            else if (e.Data.GetDataPresent(DataFormats.Text))
            {
                string droppedText = e.Data.GetData(DataFormats.Text, true) as string;
                if (Uri.IsWellFormedUriString(droppedText, UriKind.Absolute))
                {
                    LinkedFiles.Add(new DataClasses.LinkedFile(droppedText, true));
                }
            }
        }

        private void lstFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstFiles.SelectedItems.Count == 1)
            {
                DataClasses.LinkedFile dropLink = (DataClasses.LinkedFile)lstFiles.SelectedItem;

                if (dropLink.FileFound || dropLink.IsURL)
                {
                    Process fileProcess = new Process();
                    fileProcess.StartInfo.FileName = dropLink.FilePath;
                    fileProcess.StartInfo.UseShellExecute = true;
                    fileProcess.Start();
                }
                else
                {
                    if (MessageBox.Show("The file was not found. It may have been moved or renamed.\nWould you like to search for it?", "File Not Found", MessageBoxButton.YesNoCancel, MessageBoxImage.Error)
                        == MessageBoxResult.Yes)
                    {
                        Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
                        ofd.Filter = "All Files (*.*)|*.*";
                        ofd.FileName = dropLink.FilePath;
                        if (ofd.ShowDialog() == true)
                        {
                            dropLink.FilePath = ofd.FileName;
                        }
                    }
                }
            }
        }

        private void lstFiles_DragOver(object sender, DragEventArgs e)
        {
            //try
            //{
            //    FrameworkElement dropElement = (FrameworkElement)e.OriginalSource;
            //    if (dropElement.DataContext != null && dropElement.DataContext.GetType() == typeof(DataClasses.Activity))
            //    {
            //        e.Effects = DragDropEffects.Link;
            //    }
            //    else
            //    {
            //        e.Effects = DragDropEffects.None;
            //        e.Handled = true;
            //    }
            //}
            //catch
            //{
            //    e.Effects = DragDropEffects.None;
            //    e.Handled = true;
            //}
        }

        private void mniAddURL_Click(object sender, RoutedEventArgs e)
        {
            Windows.URLWindow ourl = new Windows.URLWindow();
            ourl.Owner = this;
            if (ourl.ShowDialog() == true)
            {
                LinkedFiles.Add(new DataClasses.LinkedFile(ourl.txtURL.Text, true));
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            //AssignmentsManage.WindowsLive.MainWindow winlive = new WindowsLive.MainWindow();
            //winlive.ShowDialog();
        }
    }
}
